package severlet;

import bean.LeaveReturn;
import dao.LeaveReturnDao;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet(name = "AdminLeavereturnButtonServlet", value = "/AdminLeavereturnButtonServlet")
public class AdminLeavereturnButtonServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8"); // 设置编码格式
        request.setCharacterEncoding("utf-8");

        //判断提交的是哪个按钮
        String button = request.getParameter("submit");
        String studentname = request.getParameter("studentname");
        String dormitoryid = request.getParameter("dormitoryid");
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date leavetime=null;
        Date returntime=null;
        try {
             leavetime = sdf.parse(request.getParameter("leavetime"));
             returntime = sdf.parse(request.getParameter("returntime"));

        } catch (ParseException e) {
            e.printStackTrace();
            System.out.println("=============输入时间有误==============");
            request.getRequestDispatcher("FanHui.jsp").forward(request,response);
        }
        //封装
        LeaveReturn leaveReturn=new LeaveReturn();
        leaveReturn.setStudentname(studentname);
        leaveReturn.setDormitoryid(dormitoryid);
        leaveReturn.setLeavetime(leavetime);
        leaveReturn.setReturntime(returntime);
        //判断按钮 调用dao层
        LeaveReturnDao leaveReturnDao=new LeaveReturnDao();
        if(button.equals("submit")){
            leaveReturnDao.submit(leaveReturn);
        }else if (button.equals("modify")){
            leaveReturnDao.changeTime(leaveReturn);
        }
        //请求转发
        request.getRequestDispatcher("/AdminLeavereturnServlet").forward(request,response);
    }
}
